﻿
namespace Sai.Core.Rules
{
    using System.Linq;
    using Sai.Core.Introspection;

    public class CheckTypeFinalizer : IClassRule
    {
        public void Validate(ClassDeclaration was, ClassDeclaration isNow, ITypeDifference typeDifference)
        {
            FinalizerDeclaration wasDestructor = was.Finalizer;
            FinalizerDeclaration nowDestructor = isNow.Finalizer;

            if (wasDestructor == null && nowDestructor != null)
            {
                typeDifference.IdentifiedChange(
                    new UnaryTypeChange(
                        TypeChangeReason.FinalizerAdded,
                        nowDestructor,
                        "Finalizer was added"));
            }
            else if (wasDestructor != null && nowDestructor == null)
            {
                // removed
                typeDifference.IdentifiedChange(
                    new UnaryTypeChange(
                        TypeChangeReason.FinalizerRemoved,
                        wasDestructor,
                        "Finalizer was removed"));
            }

        }
    }
}
